<?php

require('configs/include.php');

class c_actualizar_kernel extends super_controller {

    public function actualizar() {
        $kernel = new kernel($this->post);

        if (is_empty($kernel->get('version'))) {
            throw_exception("Debe ingresar el nombre de la version antes de continuar");
        } else if (is_empty($kernel->get('descripcion'))) {
            throw_exception("Debe ingresar la descripcion de la version antes de continuar");
        } else {
            $this->orm->connect();
            $this->orm->insert_data("normal", $kernel);
            $this->orm->close();



            ///Insertar los programas y modulos del kernel anterior apuntando a este nuevo kernel

            $options['kernel']['lvl2'] = "by_last";
            $this->orm->connect();
            $this->orm->read_data(array("kernel"), $options);
            //Id del nuevo kernel
            $id_kernel = $this->orm->get_objects("kernel")[0]->get('id');

            $this->orm->close();

            ////Obteniendo los modulos x programas del kernel anterior
            $cod['modulo']['kernel'] = (int) $this->post->kernel; //id del kernel anterior
            $options['modulo']['lvl2'] = "by_kernel";
            $options['programa']['lvl2'] = "all";
            $components['modulo']['programa'] = array("m_p");
            $this->orm->connect();
            $this->orm->read_data(array("modulo", "programa"), $options, $cod);


            $modulo_c = $this->orm->get_objects("modulo", $components);
           // print_r2($modulo_c);
          //  $id_modulo
          // 
           if (is_empty($modulo_c)) {
                   throw_exception("El kernel sobre el que desea actualizar no tiene modulos, Llamar al administrador");
            } else {
                //print_r2($modulo_c);
                $this->orm->close();
                /// Copiando e insertando los nuevos modulos y programas apuntando al nuevo kernel 
              
                foreach ($modulo_c as $M) {
                    $M->set('kernel', $id_kernel);
                    $M->set('id', NULL);


                    $this->orm->connect();
                    $this->orm->insert_data("normal", $M);
                    $this->orm->close();

                    $options['modulo']['lvl2'] = "by_last";
                    $this->orm->connect();
                    $this->orm->read_data(array("modulo"), $options);
                    //Id del nuevo modulo
                    $id_modulo = $this->orm->get_objects("modulo")[0]->get('id');
                    print_r2($id_modulo);
                    $this->orm->close();

                 if(is_empty($M->components['m_p'])){
                       throw_exception("El kernel presenta uno o varios módulos sin programas, Inconsistencia, Llamar al administrador y
                           reportar el kernel");
                     
                     
                     
                 }else{
                    foreach ($M->components['m_p'] as $programa) {
                        $programa->set('modulo', $id_modulo);
                        $programa->set('id', NULL);
                        $this->orm->connect();
                       print_r2($programa);
                        $this->orm->insert_data("normal2", $programa);
                        $this->orm->close();
                 }}
                }
            }


            //////////////////////////////////////////////
       
             $this->type_warning = "success";
            $this->msg_warning = "Se actualizó correctamente el kernel";
            $this->img_warning = "correct.png";
            $this->engine->assign('img_warning', $this->img_warning);
            $this->temp_aux = 'message.tpl';

            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
            
            
                    }
    }

    public function display() {
            $options['kernel']['lvl2'] = "by_stateF";
            $this->orm->connect();
            $this->orm->read_data(array("kernel"), $options);
            $kernels = $this->orm->get_objects("kernel");
            $this->engine->assign('kernels', $kernels);
            $this->orm->close();
            
            $varAux = $this->gvar['n_index'] . " -> " . $this->gvar['n_update'];
            $this->engine->assign('ubicationPath', $varAux);
            $this->engine->display('header.tpl');
            $this->engine->display($this->temp_aux);
            $this->engine->display('actualizar_kernel.tpl');
            $this->engine->display('footer.tpl');
    }

    public function run() {
        if ($_SESSION['usuario']['tipo'] == "lider") {
            try {
                if (isset($this->get->option)) {
                    $this->{$this->get->option}();
                }
            } catch (Exception $e) {
                $this->error = 1;
                $this->msg_warning = $e->getMessage();
                $this->img_warning = "incorrect.png";
                $this->engine->assign('type_warning', $this->type_warning);
                $this->engine->assign('msg_warning', $this->msg_warning);
                $this->engine->assign('img_warning', $this->img_warning);
                $this->temp_aux = 'message.tpl';
            }
            $this->display();
        }else {
            $this->engine->display('header.tpl');
            $this->engine->display('sinPermisos.tpl');
            $this->engine->display('footer.tpl');
        }
    }

}

$call = new c_actualizar_kernel;
$call->run();
?>
